
package siteanaliz;

import java.lang.Thread;
import java.io.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marie
 */
public class JobRunnable implements Runnable{
    private static Object oo=new Object();
    private static int Deep;
    private static String Lang;
    private String dom;
    private String key;
    private String SE;
    private static JTable tablet;
    private static JTextArea f;
    private static JComboBox g;
   private static JComboBox h;
   private static JTextField j;
   private static JRadioButton k;
   private static JRadioButton  l;
    private static JButton  p;
    public JobRunnable(String lang, int deep, String SEG, String Domain, String keyword, JTable table0, JTextArea f1,JComboBox g1, JComboBox h1,JTextField j1, JButton p1, JRadioButton k1,JRadioButton  l2)
    {
        Lang=lang;
        Deep=deep;
        SE=SEG;
        dom=Domain;
        key=keyword;
        tablet=table0;
        f=f1;
        g=g1;
        h=h1;
        j=j1;
        k=k1;
        l=l2;
        p=p1;
    }
    public void run()
    {
            try
            {
                System.out.println(Thread.currentThread().getName());
               // double t1=System.currentTimeMillis(),t2=System.currentTimeMillis();
                Searcher ss=new Searcher(this.SE,this.Lang);
                ParseEngine seodigit=new ParseEngine(SE);
                String p1=seodigit.GetPosition(ss, Deep, dom, key);
                 f.setEnabled(false);
            g.setEnabled(false);
            h.setEnabled(false);
            j.setEnabled(false);
            k.setEnabled(false);
            l.setEnabled(false);
            p.setEnabled(false);
                int rr=iterocunt(p1);
                System.out.println("количество позиций "+rr);
                if(rr==-1 || rr==-2)
                    addPosition(dom,p1 ,SE, key, tablet );
                else
                {
                    for(int k=1;k<=rr;k++)
                    {
                        System.out.println(Thread.currentThread().getName()+" get position: "+countnum(p1,k)+" for tag "+key);
                        addPosition(GetUrl(p1,k),countnum(p1,k) ,SE, key, tablet );
                    }
                }
               // Thread.currentThread().sleep(2000); 
                
            }
            catch(Exception e)
            {
                System.out.println("inthread exeption");
            }
            finally
            {
                f.setEnabled(true);
                g.setEnabled(true);
                h.setEnabled(true);
                j.setEnabled(true);
                k.setEnabled(true);
                l.setEnabled(true);
                p.setEnabled(true);
            }
            
    }
    private int iterocunt(String ss)
    {
        if(ss.equals("unknown")) return -1;
        if(ss.equals("Captcha")) return -2;
        int firstI=0;
        int secondI=0;
        int count=0;
        while(firstI!=-1)
        {
            firstI=ss.indexOf("@",secondI);
            secondI=ss.indexOf("@",firstI+1)+1;
            // System.out.println(ss.substring(firstI+1, secondI-1));
            count++;
        }
        return count-1;
       
    }
    
    private String countnum(String ss,int count)
    {
        int firstI=0;
        int secondI=0;
        for(int i=0; i<count; i++)
        {
            firstI=ss.indexOf("@",secondI)+1;
            secondI=ss.indexOf("@",firstI)+1;
            //System.out.println(ss.substring(firstI, secondI-1));
        }
        return ss.substring(firstI, secondI-1);
    }
    
    private String GetUrl(String ss,int count )
    {
        int firstI=0;
        int secondI=0;
        for(int i=0; i<count; i++)
        {
            firstI=ss.indexOf("$U",secondI)+2;
            secondI=ss.indexOf("$U",firstI)+2;
            //System.out.println(ss.substring(firstI, secondI-1));
        }
        return ss.substring(firstI, secondI-2);
    }
    
      private void addPosition(String url, String position,String engine, String tag, JTable table1 ) {
 // Добавить URL в таблицу совпадений.
          synchronized (oo)
        {
            DefaultTableModel model =
            (DefaultTableModel) table1.getModel();
            model.addRow(new Object[]{position,engine,tag,url});
        }
        }
  
    }
   

    
   

